package concurrent;

/**
 * @Author: huangkunming
 * @Date: 2023/7/18 19:02
 * @Description:
 * 查看字节码信息
 * 1、执行javac编译成.class文件
 * 2、进入target目录下找到对应的.class文件
 * 3、执行javap -c -s -v -l .\SynchronizedDemo.class
 */
public class SynchronizedDemo {

    public void method() {
        synchronized (this) {
            System.out.println("test");
        }
    }
}

/*
* 字节码信息中，使用了monitorenter和monitorexit
* 每个对象内置了ObjectMonitor对象，在执行monitorenter和monitorexit分别会获取和释放该对象的锁。
* */

/**
 * PS D:\personal_workspace\study_notes\test\target\classes\concurrent> javap -c -s -v -l .\SynchronizedDemo.class
 * Classfile /D:/personal_workspace/study_notes/test/target/classes/concurrent/SynchronizedDemo.class
 *   Last modified 2023-7-18; size 626 bytes
 *   MD5 checksum 68a5c4f38f011b09be60ea8130d1fd72
 *   Compiled from "SynchronizedDemo.java"
 * public class concurrent.SynchronizedDemo
 *   minor version: 0
 *   major version: 52
 *   flags: ACC_PUBLIC, ACC_SUPER
 * Constant pool:
 *    #1 = Methodref          #6.#21         // java/lang/Object."<init>":()V
 *    #2 = Fieldref           #22.#23        // java/lang/System.out:Ljava/io/PrintStream;
 *    #3 = String             #24            // test
 *    #4 = Methodref          #25.#26        // java/io/PrintStream.println:(Ljava/lang/String;)V
 *    #5 = Class              #27            // concurrent/SynchronizedDemo
 *    #6 = Class              #28            // java/lang/Object
 *    #7 = Utf8               <init>
 *    #8 = Utf8               ()V
 *    #9 = Utf8               Code
 *   #10 = Utf8               LineNumberTable
 *   #11 = Utf8               LocalVariableTable
 *   #12 = Utf8               this
 *   #13 = Utf8               Lconcurrent/SynchronizedDemo;
 *   #14 = Utf8               method
 *   #15 = Utf8               StackMapTable
 *   #16 = Class              #27            // concurrent/SynchronizedDemo
 *   #17 = Class              #28            // java/lang/Object
 *   #18 = Class              #29            // java/lang/Throwable
 *   #19 = Utf8               SourceFile
 *   #20 = Utf8               SynchronizedDemo.java
 *   #21 = NameAndType        #7:#8          // "<init>":()V
 *   #22 = Class              #30            // java/lang/System
 *   #23 = NameAndType        #31:#32        // out:Ljava/io/PrintStream;
 *   #24 = Utf8               test
 *   #25 = Class              #33            // java/io/PrintStream
 *   #26 = NameAndType        #34:#35        // println:(Ljava/lang/String;)V
 *   #27 = Utf8               concurrent/SynchronizedDemo
 *   #28 = Utf8               java/lang/Object
 *   #29 = Utf8               java/lang/Throwable
 *   #30 = Utf8               java/lang/System
 *   #31 = Utf8               out
 *   #32 = Utf8               Ljava/io/PrintStream;
 *   #33 = Utf8               java/io/PrintStream
 *   #34 = Utf8               println
 *   #35 = Utf8               (Ljava/lang/String;)V
 * {
 *   public concurrent.SynchronizedDemo();
 *     descriptor: ()V
 *     flags: ACC_PUBLIC
 *     Code:
 *       stack=1, locals=1, args_size=1
 *          0: aload_0
 *          1: invokespecial #1                  // Method java/lang/Object."<init>":()V
 *          4: return
 *       LineNumberTable:
 *         line 8: 0
 *       LocalVariableTable:
 *         Start  Length  Slot  Name   Signature
 *             0       5     0  this   Lconcurrent/SynchronizedDemo;
 *
 *   public void method();
 *     descriptor: ()V
 *     flags: ACC_PUBLIC
 *     Code:
 *       stack=2, locals=3, args_size=1
 *          0: aload_0
 *          1: dup
 *          2: astore_1
 *          3: monitorenter
 *          4: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
 *          7: ldc           #3                  // String test
 *          9: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
 *         12: aload_1
 *         13: monitorexit
 *         14: goto          22
 *         17: astore_2
 *         18: aload_1
 *         19: monitorexit
 *         20: aload_2
 *         21: athrow
 *         frame_type = 250 // chop
 * offset_delta=4
 * }
 * SourceFile:"SynchronizedDemo.java"
 *
 * */
